//
//  HamatonCarListViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/3/26.
//

import UIKit
import SnapKit

typealias HamatonManagementListViewSelect = (HamatonModel) -> Void


class HamatonManagementListViewController: HamatonBaseViewController {

    // MARK: 返回事件闭包回调
    var backAction: HamatonManagementListViewSelect?
    
    var listArr :[HamatonModel] = []

    lazy var tableView: UITableView = {
        let tv = UITableView.init(frame: CGRect.zero, style: .plain)
        tv.backgroundColor = .white
        tv.separatorStyle = .none
        tv.delegate = self
        tv.dataSource = self
        tv.register(HamatonManagementListTableViewCell.self, forCellReuseIdentifier: "HamatonManagementListTableViewCell")
        view.addSubview(tv)
        return tv
    }()
    
    // 测试数据
    func ceshiShuju() {
        let gModel: HamatonModel = HamatonModel(id: "13", name: "高新区第一网格")
        listArr.append(gModel)
        let gModel1: HamatonModel = HamatonModel(id: "1", name: "高新区第2网格")
        listArr.append(gModel1)
        let gModel2: HamatonModel = HamatonModel(id: "3", name: "高新区第3网格")
        let gModel3: HamatonModel = HamatonModel(id: "31", name: "高新区第4网格")
        listArr.append(gModel2)
        listArr.append(gModel3)

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        navigationItem.title = "选择所属网格"
        ceshiShuju()
        
        tableView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension HamatonManagementListViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HamatonManagementListTableViewCell", for: indexPath) as! HamatonManagementListTableViewCell
        if indexPath.row < listArr.count {
            cell.listModel = listArr[indexPath.row]
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    

    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row < listArr.count {
            if backAction != nil {
                backAction!(listArr[indexPath.row])
            }
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    
}
